<?php

/**
 * 夏日葵电商系统
 * http://www.xiarikui.com
 */

namespace App\Model;

use Think\Model\RelationModel;

/**
 * Class AnswerModel
 * @package Home\Model
 */
class AnswerModel extends RelationModel {

    public function __construct() {
        parent::__construct();
        $this->user_id = session('user_id');
    }

    public function getInfoByQuestionId($id) {
        $id = $id ? $id : 0;
        $find = M('answer')->alias('a')->join('left join __ACCOUNTANT_USERS__ as au on a.accountant_id = au.au_id and au.status = 1')
            ->field('a.*,au.username,au.mobile,au.sex,au.head_pic')
            ->where('a.question_id = '.$id. ' and a.accountant_id = '.$this->user_id )->find();

        if($find){
            $find['head_pic'] =  (strpos($find['head_pic'],'http') === false && $find['head_pic']) ? SITE_URL.$find['head_pic'] : $find['head_pic'];
            $find['add_time'] = date('Y-m-d H:i',$find['add_time']);
            $find['adoption_num'] = $this->getAdoptionNum($find['question_id'],$find['accountant_id']);
            return $find;
        }
        return false;
    }

    public function getAdoptionNum($question_id,$au_id) {
        $question_id = $question_id ? $question_id : 0;
        $au_id = $au_id ? $au_id : 0;
        $count = M('adoption')->where('question_id = '.$question_id.' and accountant_id = '.$au_id)->count();
        return $count;
    }

    public function answer($post){
        $question_id = $post['question_id'] ? $post['question_id'] : 0 ;
        $respond_content = $post['respond_content'] ? $post['respond_content'] : 0 ;
        $price = $post['price'] ? (float)$post['price'] : 0 ;

        $res = array('status' => 0, 'msg' => '');
        if(!$question_id || !$respond_content){
            $res['msg'] = '提交参数不齐全，请刷新页面重试';
            return $res;
        }

        $count = M('question_answer')->where('question_id = '.$question_id.' and answer_id = '.$this->user_id)->count();
        if(!$count){
            $res['msg'] = '此问题并不是由您来回答';
            return $res;
        }

        $question = M('question')->where('id = '.$question_id)->find();
        if(!$question){
            $res['msg'] = '此问题记录无效';
            return $res;
        }

        if(time() >= $question['stop_time']){
            $res['msg'] = '此问题截止时间已到期，无法作答';
            return $res;
        }

        if($question['question_status'] == 1 && $question['open_reward']){
            //待支付
            $res['msg'] = '此问题开启了悬赏，仍未支付，无法作答';
            return $res;
        }

        $hasAnswer = M('answer')->where('question_id = '.$question_id)->count();
        if($question['answer_status'] == 2 || $hasAnswer > 0){
            $res['msg'] = '此问题已有回答记录或状态不符，无法作答';
            return $res;
        }

        $Answer = M('answer');
        $Question = M('question');
        $Answer->startTrans();
        $Question->startTrans();

        $bool = $Answer->data(array(
            'question_id' => $question_id,
            'accountant_id' => $this->user_id,
            'charge_view' => $price > 0 ? 1 : 0,
            'charge_money' => (float)$price,
            'content' => htmlspecialchars($respond_content),
            'add_time' => time(),
        ))->add();
        if(!$bool){
            $Answer->rollback();
            $res['msg'] = '数据保存有误，回答失败';
            return $res;
        }


        $bool = $Question->where('id = '.$question_id)->data(array(
            'answer_status' => 2,
            'question_status' => 3,
            'response_sec' => time()-$question['add_time'],
        ))->save();
        if(!$bool){
            $Answer->rollback();
            $Question->rollback();
            $res['msg'] = '数据保存有误，回答失败';
            return $res;
        }

        //将悬赏的钱打入回答者账户
        if($question['open_reward']){
            $log['question_id'] = $question_id;
            $log['au_id'] = $this->user_id;
            $log['user_id'] = $question['user_id'];
            $log['status'] = 1;
            $log['type'] = 0;
            $bool = M('pay_log')->where($log)->save(array('type' => 1));
            if($bool) {
                $log['type'] = 1;
                $rate = M('pay_log')->where($log)->getField('rate');
                M('accountant_users')->where('au_id = ' . $this->user_id)->setInc('reward_money', number_format(($question['reward_money'] * $rate / 100),2));
            }else{
                $Answer->rollback();
                $Question->rollback();
                $res['msg'] = '此问题没有悬赏的支付记录，无法作答';
                return $res;
            }
        }

        $Question->commit();
        $Answer->commit();

        $res['status'] = 1;
        $res['msg'] = '回答成功！';
        return $res;

    }


}
